﻿// See https://aka.ms/new-console-template for more information
using CommonLib;
using ConsoleAppConsumer;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System.Text;
using System.Threading.Channels;

Console.WriteLine("AppConsumer1已启动");

#region 简单模式

//var factory = new ConnectionFactory();
//factory.HostName = "localhost";
//factory.UserName = "wja";
//factory.Password = "123456";
////factory.DispatchConsumersAsync = true;
//string exchangeName = "exchange1";
//string eventName = "MyEvent";
//////using var connection = factory.CreateConnection();
//////using var channel = connection.CreateModel();
//var channel = RabbitMQUtil.CreateChannel();

//string queueName = "MyQueue1";
//channel.ExchangeDeclare(exchangeName, "direct");
//channel.QueueDeclare(queueName, durable: true, exclusive: false, autoDelete: false, arguments: null);
//channel.QueueBind(queueName, exchangeName, eventName);
//var consumer = new AsyncEventingBasicConsumer(channel);
//consumer.Received += Consumer_Received;
//channel.BasicConsume(queueName, autoAck: false, consumer: consumer);
//Console.ReadLine();

//async Task Consumer_Received(object sender, BasicDeliverEventArgs @event)
//{
//    try
//    {
//        var bytes = @event.Body.ToArray();
//        string msg = Encoding.UTF8.GetString(bytes);
//        Console.WriteLine(DateTime.Now + "收到了消息：" + msg);
//        channel.BasicAck(@event.DeliveryTag, multiple: false);
//        await Task.Delay(1000);
//    }
//    catch (Exception ex)
//    {
//        channel.BasicReject(@event.DeliveryTag, true);
//        Console.WriteLine("处理收到的消息出错" + ex);
//    }
//}
#endregion

#region 工作模式

//var queueName2 = "workQueues";
//// 获取channel
//var workChannel = RabbitMQUtil.CreateChannel();

//var consumer2 = new AsyncEventingBasicConsumer(workChannel);
//consumer2.Received += Consumer_Received;
//// 消费消息
//workChannel.BasicConsume(queueName2, autoAck: false, consumer2);
//Console.ReadLine();
//async Task Consumer_Received(object sender, BasicDeliverEventArgs @event)
//{
//    try
//    {
//        var bytes = @event.Body.ToArray();
//        string msg = Encoding.UTF8.GetString(bytes);
//        Console.WriteLine("Consumer1 收到了消息：" + msg);
//        workChannel.BasicAck(@event.DeliveryTag, multiple: false);
//        await Task.Delay(1000);
//    }
//    catch (Exception ex)
//    {
//        workChannel.BasicReject(@event.DeliveryTag, true);
//        Console.WriteLine("处理收到的消息出错" + ex);
//    }
//}

#endregion

#region Exchange模式

/**
 * 前面两种模式，简单模式、工作模式 都没有使用Exchange 交换机，他们就是无名交换机 使用空字符串
 * Exchange的类型：直接(direct) 主题(topic) 标题(headers) 扇出(fanout)
 * fanout模式：发布订阅模式
 * 发布订阅模式：将它收到的消息广播到它知道的所有队列中。
 * 
 */

#region Fanout 发布订阅模式

//// 1 创建信道 channel
//var channel = RabbitMQUtil.CreateChannel();

//var exchangeName = "logs";
//// 2 声明交换机
//channel.ExchangeDeclare(exchangeName, type: "fanout");

//// 3 声明临时队列
//var queueName = "fanout-Queue1";//Guid.NewGuid().ToString();
//channel.QueueDeclare(queueName, true, false, false, null);

//// 4 绑定队列到交换机
//channel.QueueBind(queueName, exchangeName, "");

//// 5 消费消息
//var consumer = new AsyncEventingBasicConsumer(channel);
//consumer.Received += ComsumerMessage;
//channel.BasicConsume(queueName, autoAck:false, consumer: consumer);
//Console.ReadLine();

//async Task ComsumerMessage(object sender, BasicDeliverEventArgs args)
//{
//    try
//    {
//        var bytes = args.Body.ToArray();
//        var msg = Encoding.UTF8.GetString(bytes);
//        Console.WriteLine("Consumer1 收到了消息："+msg);
//        channel.BasicAck(args.DeliveryTag, multiple: false);
//        await Task.Delay(1000);
//    }
//    catch (Exception ex)
//    {
//        channel.BasicReject(args.DeliveryTag, true);
//        Console.WriteLine("处理消息异常："+ex.Message);
//    }
//}

#endregion

//TestFanoutPattern.TestFanout();

//TestDirectPattern.TestDirect();

//TestTopicPattern.TestTopic();

#endregion

// 测试预设值
//TestPrefetch.Test();

// 测试死信 TTL 过期
//TestDeadQueue.TestTTL();

// 测试达到最大队列
//TestDeadQueue.TestMaxQueue();

// 测试消息被拒
TestDeadQueue.TestRejectMsg();